/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doda2;

import javax.jms.MessageListener;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageFormatException;
/**
 *
 * @author Shadownest
 */
public class ServiceOperatorListener implements MessageListener
{
    private boolean reserving;
    
    public ServiceOperatorListener()
    {
        reserving = false;
    }
    
    public void onMessage(Message m)
    {
        try
        {
            if (m instanceof TextMessage)
            {
                TextMessage textMessage = (TextMessage) m;
                if (textMessage.getText().equals("Package reserved") && reserving == false)
                {
                    System.out.println("Reserving package...");
                    reserving = true;
                }
                else
                {
                    System.out.println("Reading text message: " + textMessage.getText() + " - Ignored.");
                }
            }
            else if (m instanceof MapMessage)
            {
                MapMessage mapMessage = (MapMessage) m;
                if (reserving == true)
                {
                    System.out.println("Informations about the Packages :" +
                        "\nHolidayPackageID : " + String.valueOf(mapMessage.getInt("HolidayPackageID")) +
                        "\nCustomerName : " + mapMessage.getString("CustomerName") +
                        "\nPrice : " + String.valueOf(mapMessage.getFloat("Paid")) +
                        "\n\tReserved !");
                    reserving = false;
                }
                else
                {
                    System.out.println("Received MapMessage without Reserving - Ignored.");
                }
            }
            else
            {
                System.err.println("Message is not a TextMessage/ObjectMessage");
            }
        }
        catch (MessageFormatException e)
        {
            System.err.println("Object deserialization failed: " + e.toString());
        }
        catch (JMSException e)
        {
            System.err.println("JMS exception in onMessage(): " + e.toString());
        }
        catch (Throwable t)
        {
            System.err.println("Exception in onMessage():" + t.getMessage());
        }
    }
}
